package com.headfirst.observer.version1;

/**
 * 具体的观察者类，实现了观察者接口
 * @author sunhongguang
 * @create 2022-01-18-0:56
 */
public class StatisticsDisplay implements Observer,DisplayElement {
    private float maxTemperature = 0.0f;
    private float minTemperature = 200f;
    private float temperatureSum = 0.0f;
    private int numReadings;
    private Subject weatherData;

    public StatisticsDisplay(Subject weatherData) {
        this.weatherData = weatherData;
        weatherData.registerObserver(this);
    }

    @Override
    public void display() {
        System.out.println("Avg/Max/Min temperature = "+(temperatureSum/numReadings)+"/"+maxTemperature+"/"+minTemperature);
    }

    @Override
    public void update(float temperature, float humidity, float pressure) {
        // 每次气象站更新都会调用这个方法，因此可以把每次更新的温度值求和
        temperatureSum += temperature;
        numReadings++;
        if(temperature > maxTemperature){
            maxTemperature = temperature;
        }
        if(temperature < minTemperature){
            minTemperature = temperature;
        }
        display();
    }
}
